const std = @import("std");
const jetzig = @import("jetzig");
const const_val = @import("../../utils/const_value.zig");
const myjwt = @import("../../utils/myjwt.zig");

/// 限定index action处理的请求和响应格式
/// e.g:
/// GET /admin/menu.json
pub const formats: jetzig.Route.Formats = .{
    .index = &.{.json},
};

/// 后台菜单数据(响应json)
pub fn index(req: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
    const token = req.getHeader("authorization") orelse "";
    // std.debug.print("token: {s}\n", .{token});
    // token为空时，返回401
    if (token.len == 0) {
        var root = try data.object();
        try root.put("code", 401);
        try root.put("msg", "token有误");
        try root.put("data", null);
        return req.fail(.unauthorized);
    }

    // 验证token
    const exp = myjwt.ExpectedPayload{ .iss = const_val.JWT_ISS, .sub = const_val.JWT_SUB };
    const is_ok = myjwt.verifyToken(exp, token) catch false;
    if (!is_ok) {
        var root = try data.object();
        try root.put("code", 401);
        try root.put("msg", "token验证失败");
        try root.put("data", null);
        return req.fail(.unauthorized);
    }

    const menu_str =
        \\     [
        \\     {
        \\         "id": 1,
        \\         "title": "工作空间",
        \\         "icon": "layui-icon layui-icon-console",
        \\         "type": 0,
        \\         "children": [
        \\             {
        \\                 "id": "11",
        \\                 "title": "工作台",
        \\                 "icon": "layui-icon layui-icon-console",
        \\                 "type": 1,
        \\                 "openType": "_component",
        \\                 "href": "/admin/console1"
        \\             }
        \\         ]
        \\     },
        \\     {
        \\         "id": 2,
        \\         "title": "文章管理",
        \\         "icon": "layui-icon layui-icon-auz",
        \\         "type": 0,
        \\         "children": [
        \\             {
        \\                 "id": 21,
        \\                 "title": "写文章",
        \\                 "icon": "layui-icon layui-icon-face-smile",
        \\                 "type": 1,
        \\                 "openType": "_component",
        \\                 "href": "/admin/article/write"
        \\             },
        \\             {
        \\                 "id": 22,
        \\                 "title": "文章管理",
        \\                 "icon": "layui-icon layui-icon-face-smile",
        \\                 "type": 1,
        \\                 "openType": "_component",
        \\                 "href": "/admin/article/manage"
        \\             }
        \\         ]
        \\     },
        \\     {
        \\         "id": 3,
        \\         "title": "分类管理",
        \\         "icon": "layui-icon layui-icon-auz",
        \\         "type": 0,
        \\         "children": [
        \\             {
        \\                 "id": 31,
        \\                 "title": "分类管理",
        \\                 "icon": "layui-icon layui-icon-face-smile",
        \\                 "type": 1,
        \\                 "openType": "_component",
        \\                 "href": "/admin/category/manage"
        \\             }
        \\         ]
        \\     }
        \\ ]
    ;

    // 直接通过json字符串初始化data
    try data.fromJson(menu_str);

    return req.render(.ok);
}
